﻿Imports EVOLTA.Promocion.Bl.Bc
Imports EVOLTA.Promocion.Bl.Be

Partial Class Reportes_Separaciones
    Inherits System.Web.UI.Page

    Private separacions As List(Of EseparacionVenta) = Nothing
    Private separacionVenta As New EseparacionVenta
    Private separacionVentaDetalle As List(Of EseparacionVentaDetallePagos) = Nothing

#Region "       Formulario                          "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.AddHeader("Pragma", "no-cache")
        Response.Expires = -1
        '***********************************************************'
        If Not IsPostBack Then
            cargarVendedor()
            cargarTpoOperacion()
        End If
    End Sub
    Private Sub cargarVendedor()
        selVendedor.DataSource = Rvendedor.SeleccionarXEstado(Me.Master.descEmpresa, True)
        selVendedor.DataTextField = "NombresCompletos"
        selVendedor.DataValueField = "idVendedor"
        selVendedor.DataBind()
        'selVendedor.Items.Add("TODOS")
        upVendedor.Update()
    End Sub
    Private Sub cargarTpoOperacion()
        Dim EtpoOperacion As New Ecommons
        selTpoGestion.DataSource = EtpoOperacion.tpoBusquedaSeguimiento
        selTpoGestion.DataTextField = "descripcion"
        selTpoGestion.DataValueField = "codigo"
        selTpoGestion.DataBind()
    End Sub
    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        If txtFchInicio.Text.Length > 0 AndAlso txtFchFin.Text.Length > 0 Then
            cargarPendientes()
        End If
    End Sub
    Protected Sub btnLimpiar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLimpiar.Click
        txtFchInicio.Text = ""
        upFchInicio.Update()
        txtFchFin.Text = ""
        upFchFin.Update()
        'gvwLista.DataSource = Nothing
        upConsulta.Update()
    End Sub
    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportar.Click
        separacions = Session("separacionLista")
        separacionVentaDetalle = Session("separacionVentaDetalle")

        If Not separacions Is Nothing Then
            Dim fileName As String = "ConsultaIngresosDeposito{0}.xls"
            fileName = String.Format(fileName, DateTime.Now.ToShortDateString())
            Dim Response As HttpResponse = HttpContext.Current.Response
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.ContentEncoding = System.Text.Encoding.Default
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)

            Response.Write("N°") : Response.Write(vbTab)
            Response.Write("N° Separación - venta") : Response.Write(vbTab)
            Response.Write("Fecha Separación") : Response.Write(vbTab)
            Response.Write("Cliente") : Response.Write(vbTab)
            Response.Write("Dni") : Response.Write(vbTab)
            Response.Write("Teléfonos") : Response.Write(vbTab)
            Response.Write("Inmuebles") : Response.Write(vbTab)
            Response.Write("S/. Monto") : Response.Write(vbTab)
            'Response.Write("S/. Inicial") : Response.Write(vbTab)
            'Response.Write("Fecha Inicial") : Response.Write(vbTab)
            'Response.Write("S/. Financiamiento") : Response.Write(vbTab)
            'Response.Write("Fecha Saldo") : Response.Write(vbTab)
            Response.Write("Entidad Financiera") : Response.Write(vbTab)
            Response.Write(vbCrLf)

            Dim value As String
            Dim varIdSeparacion As Int32 = 0
            For Each separacion In separacions
                value = separacion.correlativo
                Response.Write(value) : Response.Write(vbTab)

                value = separacion.idSeparacionVenta
                varIdSeparacion = separacion.idSeparacionVenta
                Response.Write(value) : Response.Write(vbTab)

                value = separacion.fchSeparacion
                Response.Write(value) : Response.Write(vbTab)

                value = separacion.nombreCompletosCliente
                Response.Write(value) : Response.Write(vbTab)

                value = separacion.Ecliente.nroDocumento
                Response.Write(value) : Response.Write(vbTab)

                value = separacion.Ecliente.Telefonos
                Response.Write(value) : Response.Write(vbTab)

                value = separacion.detalle.Trim
                Response.Write(value) : Response.Write(vbTab)

                value = String.Format("{0:#,###,##0.00}", separacion.montoDeuda)
                Response.Write(value) : Response.Write(vbTab)

                'value = separacion.ctaInicial.ToString
                'Response.Write(value) : Response.Write(vbTab)

                'value = separacion.fchCtaInicial
                'Response.Write(value) : Response.Write(vbTab)

                'value = separacion.financiamiento.ToString
                'Response.Write(value) : Response.Write(vbTab)

                'value = separacion.fchSaldoFinanciar.ToString
                'Response.Write(value) : Response.Write(vbTab)

                value = separacion.descEntidadFin
                Response.Write(value)
                Response.Write(vbCrLf)

                Dim LcobranzaDetalles = From pd In separacionVentaDetalle _
                                        Where pd.idSeparacionVenta = CType(varIdSeparacion, Int32) _
                                        Select pd

                If Not LcobranzaDetalles Is Nothing Then
                    Response.Write(vbTab)
                    Response.Write("Tipo") : Response.Write(vbTab)
                    Response.Write("Fecha Cancelacion") : Response.Write(vbTab)
                    Response.Write("Forma Pago") : Response.Write(vbTab)
                    Response.Write("Monto") : Response.Write(vbTab)
                    Response.Write(vbCrLf)

                    Dim valueDetalle As String = Nothing
                    For Each separacionDetalle In LcobranzaDetalles

                        Response.Write(vbTab)
                        value = separacionDetalle.descMotivo
                        Response.Write(value) : Response.Write(vbTab)

                        value = separacionDetalle.fchCancelacion
                        Response.Write(value) : Response.Write(vbTab)

                        value = separacionDetalle.descFormaPago
                        Response.Write(value) : Response.Write(vbTab)

                        value = String.Format("{0:#,###,##0.00}", separacionDetalle.montoPago)
                        Response.Write(value)
                        Response.Write(vbCrLf)
                    Next
                    Response.Write(vbCrLf)
                End If
            Next
            Response.End()
        Else
            Dim jsMensaje As String = Nothing
            jsMensaje = "<script>javascript:alert(' La consulta no pudo ser procesada.  Por favor inténtelo de nuevo.');</script>)"
            Controls.Add(New LiteralControl(jsMensaje))
        End If
    End Sub
    Private Sub cargarPendientes()
        With separacionVenta
            .idProyecto = Me.Master.idProyecto
            .idEtapa = Me.Master.idEtapa
            .fchInicio = txtFchInicio.Text
            .fchFin = txtFchFin.Text
            .idVendedor = selVendedor.Value
            .tpoGestion = selTpoGestion.Value
        End With
        
        separacions = RseparacionVenta.ReportePendientesPagosBest(Me.Master.descEmpresa, False, separacionVenta)
        Session("separacionLista") = separacions

        separacionVentaDetalle = RseparacionVentaDetallePagos.SeleccionarXPendientePagos(Me.Master.descEmpresa, separacionVenta)
        Session("separacionVentaDetalle") = separacionVentaDetalle

        If Not separacions Is Nothing Then
            egvEstPendiente.DataSource = From s In separacions _
                                         Group s By s.idPenMor, s.descPenMor Into Group _
                                         Select idPenMor, descPenMor, Group

            egvEstPendiente.DataBind()

            'gvwLista.DataSource = separacions
            'gvwLista.DataBind()
            upConsulta.Update()
        End If
    End Sub
    Protected Sub egvEstPendiente_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles egvEstPendiente.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")

            Dim egvLista As ExGridView.ExtGridView = CType(e.Row.FindControl("gvwLista"), ExGridView.ExtGridView)
            'egvLista.AllowPaging = True
            'egvLista.PageSize = 2

            If egvLista IsNot Nothing Then
                AddHandler egvLista.RowCreated, AddressOf gvwLista_RowCreated
                AddHandler egvLista.PageIndexChanging, AddressOf gvwLista_PageIndexChanging
            End If

            Dim strUidPenMor As String = DataBinder.Eval(e.Row.DataItem, "idPenMor").ToString()

            If strUidPenMor.Length > 0 Then
                separacions = Session("separacionLista")
                Dim Lseparacions = From pd In separacions _
                                        Where pd.idPenMor = CType(strUidPenMor, Int32) _
                                        Select pd

                If separacions.Count > 0 Then
                    egvLista.DataSource = Lseparacions
                    Dim objFila As ExGridView.ExtGridViewRow = CType(e.Row, ExGridView.ExtGridViewRow)
                    objFila.ShowExpand = True
                End If
            End If
        End If
    End Sub    
    Protected Sub gvwLista_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) 'Handles gvwLista.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")

            Dim egvEstPagoDetalle As ExGridView.ExtGridView = CType(e.Row.FindControl("egvPagoDetalle"), ExGridView.ExtGridView)
            If egvEstPagoDetalle IsNot Nothing Then
                AddHandler egvEstPagoDetalle.RowCreated, AddressOf egvPagoDetalle_RowCreated
            End If


            Dim strUidSeparacion As String = DataBinder.Eval(e.Row.DataItem, "idSeparacionVenta").ToString()

            If strUidSeparacion.Length > 0 Then
                separacionVentaDetalle = Session("separacionVentaDetalle")
                Dim LcobranzaDetalles = From pd In separacionVentaDetalle _
                                        Where pd.idSeparacionVenta = CType(strUidSeparacion, Int32) _
                                        Select pd

                If separacionVentaDetalle.Count > 0 Then
                    egvEstPagoDetalle.DataSource = LcobranzaDetalles
                    Dim objFila As ExGridView.ExtGridViewRow = CType(e.Row, ExGridView.ExtGridViewRow)
                    objFila.ShowExpand = True
                End If
            End If
        End If
    End Sub
    Protected Sub gvwLista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) 'Handles gvwLista.PageIndexChanging
        separacions = Session("separacionLista")
        Dim egvLista As ExGridView.ExtGridView = CType(sender, ExGridView.ExtGridView)
        If Not separacions Is Nothing Then
            egvLista.DataSource = separacions
            egvLista.PageIndex = e.NewPageIndex
            egvLista.DataBind()
            upConsulta.Update()
        End If
    End Sub
    Protected Sub egvPagoDetalle_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
        End If
    End Sub
#End Region

End Class
